import * as vscode from 'vscode';
import { ActiveHandle } from './models/activeHandle';
import { headerAnnotation } from './models/createAnnotation';
import { _global } from './util/CONST';

// 扩展激活 默认运行
export function activate(context: vscode.ExtensionContext) {
	// 当插件关闭时被清理的可清理列表
	try {
		_global.context = context;
		registerCommand(context) // 注册命令
		new ActiveHandle().watch() // 监听事件
	} catch (err) {
		//   handleError.showErrorMessage('fileHeader: activate context', err)
	}
}

// 扩展被禁用 调用
export function deactivate() { }

// 注册命令
function registerCommand(context: vscode.ExtensionContext) {
	const fileHeader = vscode.commands.registerCommand(
		'codecomments.fileHeader',
		() => {
			console.log("命令fileHeader");
			const editor = vscode.window.activeTextEditor; //每次运行选中文件
			headerAnnotation(editor)
		}
	)
	setTimeout(() => {
		// vscode.commands.executeCommand('editor.action.organizeImports');
	}, 3000);
	// const helloWorld = vscode.commands.registerCommand(
	// 	'codecomments.helloWorld1',
	// 	() => {
	// 		vscode.window.showInformationMessage('Hello World from codecomments!');
	// 	});

	// context.subscriptions.push(helloWorld);
	context.subscriptions.push(fileHeader);
}
//获取选中的文本
function getSelectedText() {
	const documentText = vscode.window.activeTextEditor.document.getText();
	if (!documentText)
		return '';

	var activeSelection = vscode.window.activeTextEditor.selection;
	if (activeSelection.isEmpty)
		return '';

	const selStartOffset = vscode.window.activeTextEditor.document.offsetAt(activeSelection.start);
	const selEndOffset = vscode.window.activeTextEditor.document.offsetAt(activeSelection.end);

	console.log('selStartOffset', selStartOffset);
	console.log('selEndOffset', selEndOffset);

	var selectedText = documentText.slice(selStartOffset, selEndOffset).trim();
	console.log('selectedText', selectedText);
	selectedText = selectedText.replace(/\s\s+/g, ' ');
	console.log('selectedText', selectedText);
	return selectedText;
}
globalThis['vscode'] = vscode;